/* Elasticity0_1.do (STATA)
	Estimate demand elasticity for ST debt.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Elasticity0_1, replace;

/* Define local and global variables */

do global;


/* Compute mean elasticity */

Elasticity 0 1;

local Melast = r(Melast);

/* Load data */

u "../3 Estimation/Estimation0", clear;

/* Compute gradient of mean elasticity wrt demand coefficients */

mata: Delast_b = J(0,1,.);

foreach coef of varlist b_* {;
	Elasticity 0 1 `coef';

	local Delast = r(Delast);

	mata: Delast_b = (Delast_b \ `Delast');
};

/* Load data */

u "../3 Estimation/Estimation0", clear;

/* Compute gradient of mean elasticity wrt predictability coefficients */

foreach coef of varlist c_Lrealfx {;
	Elasticity 0 1 `coef';

	local Delast = r(Delast);

	mata: Delast_c = `Delast';
};

/* Load estimated coefficients */

mata: mata matuse "../3 Estimation/Estimation0", replace;
mata: mata matuse "../3 Estimation/Prediction0";

/* Report mean elasticity */

di round(`Melast',.1);

/* Compute standard error by delta method */

mata: SEelast = sqrt(Delast_b'*Var_b1*Delast_b+Delast_c'*Var_c1*Delast_c);
mata: round(SEelast,.1);

log close;
